package com.qyhl.module_practice.substreet;

import android.os.Bundle;
import android.view.MenuItem;
import android.widget.TextView;

import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.gyf.immersionbar.ImmersionBar;
import com.qyhl.module_practice.R;
import com.qyhl.module_practice.R2;
import com.qyhl.module_practice.substreet.fragment_new.SubStreetNewFragment;
import com.qyhl.webtv.basiclib.base.BaseActivity;
import com.qyhl.webtv.basiclib.base.BaseIViewPresenter;
import com.qyhl.webtv.basiclib.utils.StringUtils;
import com.qyhl.webtv.commonlib.constant.ARouterPathConstant;
import com.qyhl.webtv.commonlib.entity.civilized.PracticeHomeMenuBean;
import com.qyhl.webtv.commonlib.utils.action.ActionConstant;
import com.qyhl.webtv.commonlib.utils.action.ActionLogUtils;

import butterknife.BindView;

/**
 * 街道分中心列表
 */
@Route(path = ARouterPathConstant.PRACTICE_SUB_STREET)
public class SubStreetMainActivity extends BaseActivity {
    @BindView(R2.id.title)
    TextView title;
    @BindView(R2.id.toolbar)
    Toolbar toolbar;

    private Fragment currentFragment;


    @Override
    protected int getContentView() {
        return R.layout.practice_activity_sub_street;
    }

    @Override
    protected void setImmersionBar(ImmersionBar mImmersionBar) {
        setDefaultImmerBar();
    }

    @Override
    protected BaseIViewPresenter initBaseIViewPresenter() {
        return null;
    }

    @Override
    protected void init() {
        String instId = getIntent().getStringExtra("id");
        String volId = getIntent().getStringExtra("volId");
        int volStatus = getIntent().getIntExtra("status", 0);
        PracticeHomeMenuBean bean = (PracticeHomeMenuBean) getIntent().getSerializableExtra("bean");
        setSupportActionBar(toolbar);
        if (bean != null && StringUtils.isNotEmpty(bean.getMenuName())) {
            title.setText(bean.getMenuName());
        } else {
            title.setText("实践阵地");
        }

        if (bean != null) {
            if (bean.getMenuStyle() == 1) {
                SubStreetMainFragment subStreetFragment = SubStreetMainFragment.newInstance(instId, volStatus, volId, bean);
                add(subStreetFragment, R.id.fragment_layout, "practice_substreet");
            } else {
                SubStreetNewFragment subStreetFragment = SubStreetNewFragment.newInstance(instId, volId, volStatus);
                add(subStreetFragment, R.id.fragment_layout, "practice_substreet");
            }
        } else {
            SubStreetMainFragment subStreetFragment = SubStreetMainFragment.newInstance(instId, volStatus, volId, bean);
            add(subStreetFragment, R.id.fragment_layout, "practice_substreet");
        }

    }

    @Override
    protected void setListener() {

    }

    public void add(Fragment fragment, int id, String tag) {
        FragmentManager mFragmentManager = getSupportFragmentManager();
        FragmentTransaction mFragmentTransaction = mFragmentManager.beginTransaction();
        Fragment tempFragment = mFragmentManager.findFragmentByTag(tag);
        if (tempFragment != null) {
            fragment = tempFragment;
        }
        if (fragment.isAdded()) {
            addOrShowFragment(mFragmentTransaction, fragment, id, tag);
        } else {
            if (currentFragment != null && currentFragment.isAdded()) {
                mFragmentTransaction.hide(currentFragment).add(id, fragment, tag).commit();
            } else {
                mFragmentTransaction.add(id, fragment, tag).commit();
            }
            currentFragment = fragment;
        }
    }

    /**
     * 添加或者显示 fragment
     *
     * @param fragment 碎片
     */
    private void addOrShowFragment(FragmentTransaction transaction, Fragment fragment, int id, String tag) {
        if (currentFragment == fragment) {
            return;
        }
        if (!fragment.isAdded()) {
            transaction.hide(currentFragment).add(id, fragment, tag).commit();
        } else {
            transaction.hide(currentFragment).show(fragment).commit();
        }
        currentFragment.setUserVisibleHint(false);
        currentFragment = fragment;
        currentFragment.setUserVisibleHint(true);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onResume() {
        super.onResume();
        ActionLogUtils.getInstance().onResume(this, ActionConstant.PRACTICE_SUB_STREET);
    }

    @Override
    protected void onPause() {
        super.onPause();
        ActionLogUtils.getInstance().onPause(this, ActionConstant.PRACTICE_SUB_STREET);
    }
}
